<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">移动电表管理系统</div>
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
      >
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="用户名">
            <template #prepend>
              <el-button :icon="User"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            v-model="param.password"
            @keyup.enter="submitForm(login)"
          >
            <template #prepend>
              <el-button :icon="Lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <!-- <div class="codeBox">
            <el-form-item prop="code">
              <el-input
                v-model.trim="param.code"
                type="text"
                autocomplete="off"
                placeholder="请输入验证码"
                maxlength="12"
                size="large"
              />
            </el-form-item>
            <div class="img" @click="getCodePictureData">
              <img style="width: 100%; height: 100%" :src="imgUrl" alt="" />
            </div>
          </div> -->
        <div class="login-btn">
          <el-button type="primary" @click="submitForm(login)">登录</el-button>
        </div>
        <!-- <p class="login-tips">Tips : 用户名和密码随便填。</p> -->
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { useTagsStore } from "../store/tags";
import { usePermissStore } from "../store/permiss";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { Lock, User } from "@element-plus/icons-vue";
import { getCodePicture, loginFn } from "../api/index";

interface LoginInfo {
  username: string;
  password: string;
  code: string;
}

onMounted(() => {
  getCodePictureData();
});

const imgUrl = ref("");
const getCodePictureData = () => {
  getCodePicture().then((res: any) => {
    const blob = new Blob([res]);
    const href = window.URL.createObjectURL(blob);
    imgUrl.value = href;
  });
};

const router = useRouter();
const param = reactive<LoginInfo>({
  username: "",
  password: "",
  code: "",
});

const rules: FormRules = {
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
};
const permiss = usePermissStore();
const login = ref<FormInstance>();
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if (valid) {
      // ElMessage.success('登录成功');
      // sessionStorage.setItem('ms_username', param.username);
      // const keys = permiss.defaultList[param.username == 'admin' ? 'admin' : 'user'];
      // permiss.handleSet(keys);
      // sessionStorage.setItem('ms_keys', JSON.stringify(keys));
      // router.push('/');
      loginFn({
        username: param.username,
        password: param.password,
        code: param.code,
      }).then((res: any) => {
        if (res.code === 200) {
          ElMessage.success("登录成功");
          sessionStorage.setItem("ms_username", res.rows.username);
          sessionStorage.setItem("user", JSON.stringify(res.rows));
          // setSession("token", "121212")
          // setSession("userInfo", res.rows)
          router.push("/");
        } else {
          ElMessage.error(res.msg);
        }
      });
    } else {
      return false;
    }
  });
};

const tags = useTagsStore();
tags.clearTags();
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100vh;
  background-image: url(../assets/img/login-bg.png);
  background-size: 100%;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}

.codeBox {
  display: flex;
}
.img {
  width: 50%;
  height: 42px;
  background-color: #06fffd;
  margin-left: 10px;
}
</style>
